package com.chat.service.message;

import com.chat.dao.MessageDao;
import com.chat.dao.UserDao;
import com.chat.entity.Message;
import com.chat.entity.User;
import com.chat.util.HibernateUtil;
import com.chat.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class MessageServiceImp implements MessageService{
    @Autowired
    private MessageDao messageDao;

    @Autowired
    private UserDao userDao;

    @Override
    public List<Message> getMessages(String sourceEmail, String targetEmail) {
        User sourceUser=userDao.getUserByEmail(sourceEmail);
        User targetUser=userDao.getUserByEmail(targetEmail);


        return messageDao.getMessages(sourceUser,targetUser);
    }

    @Override
    public void deleteMessage(long id) {
        messageDao.deleteMessage(id);
    }

    @Override
    public void sendMessage(String sourceEmail, String targetEmail, String msg) {
        User sourceUser=userDao.getUserByEmail(sourceEmail);
        User targetUser=userDao.getUserByEmail(targetEmail);

        //创建message对象并赋值
        Message message=SpringUtil.getBean(Message.class);
        message.setSourceUser(sourceUser);
        message.setTargetUser(targetUser);
        message.setText(msg);
        message.setStatus(new Byte("0"));
        message.setTime(Timestamp.valueOf(LocalDateTime.now()));


        messageDao.saveMessage(message);
    }
}
